package com.itmk.config;

import com.itmk.interceptor.JWTInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //这里配置携带token和不携带token路径。
        registry.addInterceptor(new JWTInterceptor())
//                .addPathPatterns("/**")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/admin/**")
                .excludePathPatterns("/images/**")
                .excludePathPatterns("/pdfs/**")
                .excludePathPatterns("/ws/**")
                .excludePathPatterns("/chat/**")
                .excludePathPatterns("/team-api/**")
                .excludePathPatterns("/api/**");
    }

//
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                .allowedOrigins("*")
//                .allowedHeaders("*")
//                .allowedMethods("GET","POST","DELETE","PUT","OPTIONS","HEAD");
//    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        System.out.println("配置文件已经生效");
        //关于图片上传后需要重启服务器才能刷新图片
        //这是一种保护机制，为了防止绝对路径被看出来，目录结构暴露
        //解决方法:将虚拟路径/images/

//        String property = System.getProperty("user.dir")+"\\itmk-base-web";
//        String path = property + "\\target\\classes\\static\\images\\companyLogo\\";
//
//        String property1 = System.getProperty("user.dir")+"\\itmk-base-web";
//        String path1 = property1 + "\\target\\classes\\static\\images\\userProfile\\";
//
//        String property2 = System.getProperty("user.dir")+"\\itmk-base-web";
//        String path2 = property2 + "\\target\\classes\\static\\img\\";

        String path = "E:\\file\\images\\companyLogo\\";
        String path1 = "E:\\file\\images\\userProfile\\";
        String path2 = "E:\\file\\pdfs\\resume\\";
        String path3 = "E:\\file\\images\\assets\\";

        registry.addResourceHandler("/images/companyLogo/**")
                .addResourceLocations("file:"+path);
        registry.addResourceHandler("/images/userProfile/**")
                .addResourceLocations("file:"+path1);
        registry.addResourceHandler("/pdfs/resume/**")
                .addResourceLocations("file:"+path2);
        registry.addResourceHandler("/images/assets/**")
                .addResourceLocations("file:"+path3);
    }
}
